package com.mijie.homi.admin.web.commom;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mijie.core.Paging;
import com.mijie.homi.admin.domain.commom.SensitiveWord;
import com.mijie.homi.admin.service.commom.SensitiveWordService;
import com.mijie.homi.admin.util.InternalRequest;


@Controller
@RequestMapping("/sensitiveword")
public class SensitiveWordController {

	@Autowired
	private SensitiveWordService service;
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	public String list(Paging<SensitiveWord> paging,Model model,HttpServletRequest request){
		Map<String,Object> searchBundle = (Map<String, Object>) request.getAttribute("searchBundle");
		service.listSensitiveWord(paging,searchBundle);
		model.addAllAttributes(searchBundle);
		return "sensitiveword/list";
	}
	
	@RequestMapping("/create")
	public String create(Model model){
		return "sensitiveword/create";
	}
	
	@RequestMapping("/save")
	public String save(SensitiveWord word,HttpServletRequest request){
		service.addSensitiveWord(word);
		return "redirect:/sensitiveword/list.shtml";
	}
	
	@RequestMapping("/edit")
	public String edit(Model model,String word){
		SensitiveWord sensitiveWord = service.getSensitiveWordByWord(word);
		model.addAttribute("word",sensitiveWord);
		return "sensitiveword/edit";
	}
	
	@RequestMapping("/update")
	public String update(SensitiveWord word,HttpServletRequest request){
		service.updateSensitiveWord(word);
		return "redirect:/sensitiveword/list.shtml";
	}
	
	@RequestMapping("/refresh")
	public void refresh(HttpServletResponse response) throws IOException {
		InternalRequest.request("/internal/sensitiveword/reload.shtml", "POST", "UTF-8", null);
	}
	
	@RequestMapping("/delete")
	public String delete(String word,HttpServletRequest request){
		service.deleteSensitiveWord(word);
		return "redirect:/sensitiveword/list.shtml";
	}
}
